/*
提交链接:https://leetcode.cn/problems/longest-consecutive-sequence/description/
128. 最长连续序列
赖德檀 2024/10/7
*/

class Solution {
public:
unordered_map<int,int>par;

    void Solu(vector<int>& nums)
    {
        for(int i=0;i<nums.size();i++)
        par[nums[i]]=nums[i];
    }

    int find(int x)
    {
        if(x!=par[x])
        par[x]=find(par[x]);
        return par[x];
    }

    void pair(int b,int a)
    {
        par[find(a)]=find(b);
    }

    int longestConsecutive(vector<int>& nums) 
    {
        int n=nums.size();
        Solu(nums);
        ranges::sort(nums);
        for(int i=0;i<n;i++)
        {
            if(par.find(nums[i]+1)!=par.end())
            {
                pair(nums[i],nums[i]+1);
            }
        }
        unordered_map<int,int>mp;
        int ans=0;
        for(auto it : par)
       mp[it.second]++;
       for(auto it : mp)
       ans=max(it.second,ans);
        return ans;
    }
};